package cn.edu.swpu.cins.u_chain.frontend.service.user.Impl;

import cn.edu.swpu.cins.u_chain.Exception.user.UserException;
import cn.edu.swpu.cins.u_chain.frontend.service.user.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Random;

/**
 * Created by muyi on 17-4-22.
 */
@Service
public class MailServiceImpl implements MailService {

    @Autowired
    private JavaMailSender sender;


    @Value("${spring.mail.username}")
    private String from;

    /**
     * 发送纯文本的简单邮件
     *
     * @param to
     * @param subject
     * @param content
     */

    //    获取4位随机数(验证码)
    public static final char[] chars = "1234567890QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm".toCharArray();
    public static Random random = new Random();

    public static String getRandomString() {
        StringBuffer buffer = new StringBuffer();
        int index;   //获取随机chars下标
        for (int i = 0; i < 4; i++) {
            index = random.nextInt(chars.length);  //获取随机chars下标
            buffer.append(chars[index]);
        }
        return buffer.toString();
    }


//    public String getSignUpSubject(String username) {
//        final String format = "尊敬的username，欢迎注册校园威客平台！";
//        return format.replaceAll("username", username);
//    }

    public String getSignUpContent(String status) {
        String previous;
        switch (status) {
            case "confirmMail":
                previous = "您正在UChain(优橙)高校人才平台激活您的邮箱，请您在平台验证邮箱界面输入您的验证码，倘若非您本人操作，请忽略这封邮件，验证码为：";
                return previous;

            case "changeMail":
                previous = "您正在UChain(优橙)高校人才平台更改您的绑定邮箱，请您在平台验证邮箱界面输入您的验证码，倘若非您本人操作，请忽略这封邮件，验证码为：";
                return previous;

            case "repeatCheckMail":
                previous = "您正在UChain(优橙)高校人才平台激活您的新绑定邮箱，请您在平台验证邮箱界面输入您的验证码，倘若非您本人操作，请忽略这封邮件，验证码为：";
                return previous;

            default:
                throw new UserException("服务器内部异常");
        }
    }

    @Override
    public String sendSimpleMail( String to,String status) throws Exception {
        String verifyCode = getRandomString();
//        SimpleMailMessage message = new SimpleMailMessage();
//
//        message.setFrom(from);
//        message.setTo(to);
//        message.setSubject("尊敬的用户，您好，欢迎使用UChain(优橙)高校人才平台");
//        message.setText(getSignUpContent(status) + "   " + verifyCode + "   " + ",验证码有效时长为10分钟。");
//            sender.send(message);
//
        MimeMessage mimeMessage =  sender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        String nick = javax.mail.internet.MimeUtility.encodeText("UChain(优橙)高校人才平台");
        helper.setFrom(new InternetAddress(nick + "<51uchain@sina.com>"));
        helper.setTo(to);//接收者.
        helper.setSubject("尊敬的用户，您好，欢迎使用UChain(优橙)高校人才平台");//邮件主题.
        helper.setText(getSignUpContent(status) + "   " + verifyCode + "   " + ",验证码有效时长为10分钟。");//邮件内容.
        sender.send(mimeMessage);
        return verifyCode;
    }
}
